package handler

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"math/rand"
	"net/http"
	"strconv"
	"time"
	"week61/api/request"
	"week61/model/mysql"
	"week61/model/redis"
	"week61/pkg"
)

func SendSms(c *gin.Context) {
	var data request.SendSms
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	code := rand.Intn(9000) + 1000
	key := "SendSms" + data.Tel

	sms, err := pkg.SendSms(data.Tel, strconv.Itoa(code))
	if *sms.Body.Code != "OK" {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "短信发送失败",
			"Data": nil,
		})
		return
	}

	err = redis.RDB.Set(context.Background(), key, code, time.Minute*30).Err()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "短信发送失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "短信发送成功",
		"Data": code,
	})
}

func Login(c *gin.Context) {
	var data request.Login
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	u := mysql.User{}
	login, err := u.Login(data.Tel, data.Password)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	key := "SendSms" + data.Tel
	if redis.RDB.Get(context.Background(), key).Val() != data.Code {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "验证码错误",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "登录成功",
		"Data": login,
	})
}

func Register(c *gin.Context) {
	var data request.Register
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}

	tel := pkg.LookTel(data.Tel)
	if !tel {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "手机号格式错误",
			"Data": nil,
		})
		return
	}

	u := mysql.User{
		Tel:      data.Tel,
		Password: data.Password,
	}
	err = u.Register()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "注册成功",
		"Data": u.ID,
	})
}

func AddOrder(c *gin.Context) {
	var data request.AddOrder
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "参数错误",
			"Data": nil,
		})
		return
	}
	g := mysql.Goods{}
	findGoods, err := g.FindGoods(data.Gid)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	Sn := uuid.New().String()
	o := mysql.Order{
		Uid:        data.Uid,
		Gid:        data.Gid,
		OrderSn:    Sn,
		GoodsName:  findGoods.Name,
		GoodsPrice: findGoods.Price,
		GoodsNum:   findGoods.Num,
		PayPrice:   findGoods.Price * float64(findGoods.Num),
	}
	err = o.AddOrder()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "订单生成成功",
		"Data": o.ID,
	})
}

func OrderList(c *gin.Context) {
	o := mysql.Order{}
	order, err := o.FindOrder()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "成功",
		"Data": order,
	})
}
